package com.ruoyi.rcpt.rczc.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.rcpt.rczc.domain.RczcFj;
import com.ruoyi.rcpt.rczc.service.IRczcService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.rcpt.rczc.domain.Rczc;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人才政策Controller
 *
 * @author huacai
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/rcpt/rczc")
public class RczcController extends BaseController
{
    @Resource
    private IRczcService rczcService;

    /**
     * 查询人才政策列表
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rczc:list')")
    @GetMapping("/list")
    public TableDataInfo list(Rczc rczc)
    {
        startPage();
        List<Rczc> list = rczcService.selectRczcList(rczc);
        return getDataTable(list);
    }

    /**
     * 导出人才政策列表
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rczc:export')")
    @Log(title = "人才政策", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Rczc rczc)
    {
        List<Rczc> list = rczcService.selectRczcList(rczc);
        ExcelUtil<Rczc> util = new ExcelUtil<Rczc>(Rczc.class);
        util.exportExcel(response, list, "人才政策数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Rczc> util = new ExcelUtil<Rczc>(Rczc.class);
        util.importTemplateExcel(response, "人才政策数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "人才政策", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('rcpt:rczc:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Rczc> util = new ExcelUtil<Rczc>(Rczc.class);
        InputStream inputStream = file.getInputStream();
        List<Rczc> list = util.importExcel(inputStream );
        inputStream.close();
        int count = rczcService.batchInsertRczc(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取人才政策详细信息
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rczc:query')")
    @GetMapping(value = "/{rczcId}")
    public AjaxResult getInfo(@PathVariable("rczcId") String rczcId)
    {
        return success(rczcService.selectRczcByRczcId(rczcId));
    }

    /**
     * 新增人才政策
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rczc:add')")
    @Log(title = "人才政策", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Rczc rczc)
    {
        rczc.setUserId(Math.toIntExact(getUserId())); //用户ID
        rczc.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        rczc.setCreateBy(getUsername()); //创建人
        rczc.setIdWhere("存在");
        return toAjax(rczcService.insertRczc(rczc));
    }

    /**
     * 修改人才政策
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rczc:edit')")
    @Log(title = "人才政策", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Rczc rczc)
    {
        return toAjax(rczcService.updateRczc(rczc));
    }

    /**
     * 删除人才政策
     */
    @PreAuthorize("@ss.hasPermi('rcpt:rczc:remove')")
    @Log(title = "人才政策", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rczcIds}")
    public AjaxResult remove(@PathVariable String[] rczcIds)
    {
        return toAjax(rczcService.deleteRczcByRczcIds(rczcIds));
    }

    /**
     * 获得执行填报主键ID (UUID)
     */
    @GetMapping("/getRcZcId")
    public AjaxResult getRcZcId(){
        String getRcZcId = IdUtils.fastSimpleUUID();
        return success(getRcZcId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertRczcFjByRczcId(@RequestBody RczcFj rczcFj){
        return toAjax(rczcService.insertRczcFjByRczcId(rczcFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectRczcFjList(String sId){
        RczcFj rczcFj = new RczcFj();
        rczcFj.setRczcId(sId);
        return AjaxResult.success(rczcService.selectRczcFjList(rczcFj));
    }

    /**
     * 删除人才政策附件
     */
    @Log(title = "人才政策附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fjs/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds)
    {
        return toAjax(rczcService.deleteRczcFjByFjIds(fjIds));
    }

}
